<template>
	<view class="container">
		<view class="logs">
			<view class="cu-list menu">
				<view class="cu-item padding-sm" v-for="(item, index) in list" :key="index">
					<view class="content">
						<view>
							<text>{{ item.reason }}</text>
						</view>
						<view>
							<text class="text-gray">{{ item.create_time }}</text>
						</view>
					</view>
					<view class="action padding-right-sm">
						<view v-if="item.type == 1" class="text-red text-lg">+{{ item.money }}</view>
						<view v-if="item.type == 2" class="text-gray text-lg">-{{ item.money }}</view>
					</view>
				</view>
			</view>
			<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				income: 0.00,
				expend: 0.00,
				page: 1,
				lastPage: 0,
				list: [],
				loadMoreText: "加载中...",
				showLoadMore: false
			};
		},
		onUnload() {
			this.page = 1,
			this.lastPage = 0,
			this.list = [],
			this.loadMoreText = "加载更多",
			this.showLoadMore = false;
		},
		onLoad: function (option) {
			this.helper.checkLogin();
			this.getData();
		},
		onReachBottom() {
			// console.log("onReachBottom");
			if (this.page > this.lastPage) {
				this.loadMoreText = "没有更多数据了!"
				return;
			}else{
				this.showLoadMore = true;
				setTimeout(() => {
					this.getData();
				}, 300);
			}
		},
		onPullDownRefresh() {
			this.page = 1,
			this.lastPage = 0,
			this.list = [],
			this.loadMoreText = "加载更多",
			this.showLoadMore = false;
			
			setTimeout(() => {
				this.getData();
			}, 300);
		},
		methods: {
			getData:function(){
				//获取基础参数
				uni.showLoading({
					title:"数据加载中",
					mask:true
				})
				let page = this.page;
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['token='+info.token,'page='+page,'timestamp='+timestamp]);
				this.helper.req({
					api:"getBill",
					timestamp:timestamp,
					data:{token:info.token,page:page},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					let current_page = res.data.data.list.current_page;
					let last_page = res.data.data.list.last_page;
					this.page = current_page + 1;
					this.lastPage = last_page;
					this.list = this.list.concat(res.data.data.list.data);
					if (this.page >= this.lastPage) {
						this.loadMoreText = "没有更多数据了!"
					}
					this.income = res.data.data.income;
					this.expend = res.data.data.expend;
					
					uni.stopPullDownRefresh();
					uni.hideLoading();
				})
			},
			//链接
			toUrl:function(url){
				// console.log(url);
				uni.navigateTo({
					url: url,
				});	
			},
		},
	};
</script>

<style lang="scss">
.cu-list.menu > .cu-item{
	padding: 20upx;
}
</style>